const DatabasePool = require('./utils/dbPool.cjs');
const path = require('path');

async function checkStaffFields() {
  const dbPool = new DatabasePool(path.join(__dirname, 'data', 'pph.sqlite'));

  try {
    console.log('=== PPH Records 表结构 - 人员相关字段 ===');

    // 等待连接池初始化
    await new Promise(resolve => setTimeout(resolve, 2000));

    // 先检查表是否存在
    const tableExists = await dbPool.query("SELECT name FROM sqlite_master WHERE type='table' AND name='pph_records'");
    console.log('表存在检查:', tableExists);

    if (!Array.isArray(tableExists) || tableExists.length === 0) {
      console.log('pph_records 表不存在');
      return;
    }

    // 使用SELECT查询来获取表结构信息
    const rows = await dbPool.query("SELECT sql FROM sqlite_master WHERE type='table' AND name='pph_records'");
    console.log('表结构SQL:', rows);

    // 尝试查询实际数据来了解字段
    const sampleData = await dbPool.query('SELECT * FROM pph_records LIMIT 1');
    console.log('样本数据:', sampleData);

    if (Array.isArray(sampleData) && sampleData.length > 0) {
      const fields = Object.keys(sampleData[0]);
      console.log('所有字段:', fields);

      const staffFields = fields.filter(field =>
        field.includes('people') ||
        field.includes('staff') ||
        field.includes('online') ||
        field.includes('offline') ||
        field.includes('working')
      );

      console.log('人员相关字段:', staffFields);
    }

    // 尝试查询人员字段的实际数据
    console.log('\n=== 查询实际数据样本 ===');
    try {
      const dataRows = await dbPool.query('SELECT id, offline_staff, total_people, working_people FROM pph_records LIMIT 3');

      if (!Array.isArray(dataRows) || dataRows.length === 0) {
        console.log('没有找到记录');
      } else {
        console.log('样本数据:');
        dataRows.forEach(row => {
          console.log(`ID ${row.id}: working_people=${row.working_people}, offline_staff=${row.offline_staff}, total_people=${row.total_people}`);
        });
      }
    } catch (error) {
      console.log('查询人员字段数据失败:', error.message);
    }

  } catch (error) {
    console.error('检查出错:', error);
  } finally {
    // 关闭连接池
    if (dbPool) {
      await dbPool.close();
    }
    process.exit(0);
  }
}

checkStaffFields();
